package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.VmStatusContract;
import com.lkd.emq.Topic;
import com.lkd.exception.LogicException;
import com.lkd.feign.UserService;
import com.lkd.feign.VMService;
import com.lkd.http.vo.TaskViewModel;
import com.lkd.service.TaskService;
import com.lkd.utils.DateUtil;
import com.lkd.utils.JsonUtil;
import com.lkd.vo.UserVO;
import com.lkd.vo.VmVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.List;

/**
 * 接收工单消息
 */
@Component
@Topic(TopicConfig.VMS_STATUS_TOPIC)
@Slf4j
public class VMStatusHandler implements MsgHandler {
    @Autowired
    private TaskService taskService;
    
    @Autowired
    private VMService vmService;

    @Autowired
    private UserService userService;

    @Override
    public void process(String jsonMsg) throws IOException {
        var contract = JsonUtil.getByJson(jsonMsg, VmStatusContract.class);
        if (contract == null || contract.getStatusInfo() == null || contract.getInnerCode() == null) {
            return;
        }
        contract.getStatusInfo().forEach(statusInfo -> {
            if (!statusInfo.isStatus()) {
                //如果为非正常状态，则创建维修工单
                //维修工单要插入到tb_task表中
                TaskViewModel taskViewModel = new TaskViewModel();
                //封装设备id
                taskViewModel.setInnerCode(contract.getInnerCode());
                //工单类别：手动1 自动0
                taskViewModel.setCreateType(0);
                //工单说明
                taskViewModel.setDesc(statusInfo.getStatusCode()+"售货机出现异常，请及时处理");
                //工单类型
                taskViewModel.setProductType(VMSystem.TASK_TYPE_REPAIR);

                VmVO vmInfo = vmService.getVMInfo(contract.getInnerCode());
                int leastUser = taskService.getLeastUser(vmInfo.getRegionId(), true);
                //分配给当天任务最少的用户
                taskViewModel.setUserId(leastUser);
            }
        });


    }

    //    private Integer findMinTaskCountUser(String innerCode) {
//        //查询所有负责改售货机维护的维修人员列表
//        List<UserVO> userVOList = userService.getRepairerListByInnerCode(innerCode);
//
//        if (CollectionUtils.isEmpty(userVOList)) {
//            throw new LogicException("该区域没有运维人员");
//        }
//
//        //查询当天每个维修人员的工单数量
//        DateTime begin = DateUtil.beginOfDay(new Date());
//        DateTime end = DateUtil.endOfDay(new Date());
//        List<TaskEntity> taskEntityList = taskService.getUserTask(begin, end);
//
//        Map<Integer, List<TaskEntity>> userCount = taskEntityList.stream().collect(Collectors.groupingBy(TaskEntity::getUserId));
//
//        List<UserTaskCountVo> countVoList = userVOList.stream().map(userVO -> {
//
//            UserTaskCountVo userTaskCountVo = new UserTaskCountVo();
//            userTaskCountVo.setUserId(userVO.getUserId());
//
//            List<TaskEntity> taskList = userCount.get(userVO.getUserId());
//
//            if (CollectionUtils.isEmpty(taskList)) {
//                userTaskCountVo.setCount(0);
//            } else {
//                userTaskCountVo.setCount(taskList.size());
//            }
//
//            return userTaskCountVo;
//        }).sorted((o1, o2) -> o1.getCount() - o2.getCount()).limit(1).collect(Collectors.toList());
//
//
//        return countVoList.get(0).getUserId();
//    }
}
